package com.zgh.init;

import com.xxg.util.SystemProperty;
import com.zgh.manager.IManagerService;
import com.zgh.manager.module.AutoTaskLog;
import com.zgh.manager.module.InterfaceLog;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;

@Service("UpLoadTask")
public class UpLoadTask implements Job {

	@Autowired
	private IManagerService managerService;

	private static final int UPLOAD_TASK_QUEUE_LENGTH=Integer.parseInt(SystemProperty.getProperty("uploadTaskQueueLength"));
	private static ArrayBlockingQueue<AutoTaskLog> autoTaskLogQueue=new ArrayBlockingQueue<AutoTaskLog>(UPLOAD_TASK_QUEUE_LENGTH);
	private static ArrayBlockingQueue<InterfaceLog> interfaceLogQueue=new ArrayBlockingQueue<InterfaceLog>(UPLOAD_TASK_QUEUE_LENGTH);

	public static void insertAutoTaskLog(AutoTaskLog autoTaskLog){
		autoTaskLogQueue.offer(autoTaskLog);
	}

	public static void insertInterfaceLog(InterfaceLog interfaceLog){
		interfaceLogQueue.offer(interfaceLog);
	}


	@Override
	public void execute(JobExecutionContext context)
			throws JobExecutionException {
		List<AutoTaskLog> autoTaskLogs=new ArrayList<AutoTaskLog>();
		for(int i=0;i<UPLOAD_TASK_QUEUE_LENGTH;i++){
			AutoTaskLog temp=autoTaskLogQueue.poll();
			if(temp==null){
				break;
			}
			autoTaskLogs.add(temp);
		}


		List<InterfaceLog> interfaceLogs=new ArrayList<InterfaceLog>();
		for(int i=0;i<UPLOAD_TASK_QUEUE_LENGTH;i++){
			InterfaceLog temp=interfaceLogQueue.poll();
			if(temp==null){
				break;
			}
			interfaceLogs.add(temp);
		}

		managerService.insertAutoTaskLog(autoTaskLogs);
		managerService.insertInterfaceLog(interfaceLogs);
	}

}
